<?php
	if (isset($_GET['group_invites'])) {
		if (isset($_GET['invitation'])) {
			if ($_POST['join']) {
				if (addGroupUser($_POST['groupid'], $_SESSION['username'], 'N')) {
					if (removeAllInvites($_SESSION['username'])) {
						echo "<p>You have joined the Flatshare Group!</p>";
					}
					else {
						echo "<p>You have joined the Flatshare Group, but there was an error removing your invitation. Please contact an administrator.</p>";
					}
				}
				else {
					echo "<p>There was an error adding you to the group. Please contact the administrator.</p>";
				}
			}
			else if ($_POST['remove']) {
				if (removeInvite($_SESSION['username'], $_POST['groupid'])) {
					echo "<p>You have removed the group invitation.</p>";
				}
				else {
					echo "<p>There was an error removing the group invitation. Please contact an administrator.</p>";
				}
			}
		}
		else {
			include("flatshare_php/group_invites.php");
		}
	}
	else if (isset($_GET['viewbills'])) {
		if (!checkUserGroup($_SESSION['username'])) {
			echo "<p>You are not currently a member of a Flatshare Group. You may create a group from the menu on the left, or wait for an invitation to join a group.</p>";
		}
		else {
			// code here to display any bills/chores
			include("flatshare_php/group_viewbills.php");	
		}
	}
	else if (isset($_GET['addbill'])) {
		if (!checkUserGroup($_SESSION['username'])) {
			echo "<p>You are not currently a member of a Flatshare Group. You may create a group from the menu on the left, or wait for an invitation to join a group.</p>";
		}
		else {
			// code for adding a bill to the system
			include("flatshare_php/group_addbill.php");	
		}
	}
	else if (isset($_GET['viewnotifications'])) {
		if (!checkUserGroup($_SESSION['username'])) {
			echo "<p>You are not currently a member of a Flatshare Group. You may create a group from the menu on the left, or wait for an invitation to join a group.</p>";
		}
		else {
			// code to display any pending notifications
			include("flatshare_php/group_notifications.php");
		}
	}
	else if (isset($_GET['trackbills'])) {
		if (!checkUserGroup($_SESSION['username'])) {
			echo "<p>You are not currently a member of a Flatshare Group. You may create a group from the menu on the left, or wait for an invitation to join a group.</p>";
		}
		else {
			// code to display any bills/chores user has entered into system
			include("flatshare_php/group_trackbills.php");
		}
	}
	else if (isset($_GET['adduser'])) {
		if (!checkUserGroup($_SESSION['username'])) {
			echo "<p>You are not currently a member of a Flatshare Group. You may create a group from the menu on the left, or wait for an invitation to join a group.</p>";
		}
		else {
			include("flatshare_php/group_adduser.php");
		}
	}
	else if (isset($_GET['removeuser'])) {
		$groupid = checkUserGroup($_SESSION['username']);
		if ($groupid == NULL || $groupid == 0) {
			echo "<p>You are not currently a member of a Flatshare Group. You may create a group from the menu on the left, or wait for an invitation to join a group.</p>";
		}
		else {
			if (checkGroupAdmin($groupid, $_SESSION['username'])) {
				// can remove users
				if (isset($_GET['removed'])) {
					// code to actually remove user from group
					if (removeUser($_POST['username'])) {
						echo "<p>The user was successfully removed from the Flatshare Group.</p>";
					}
					else {
						echo "<p>The user was NOT removed from the Flatshare Group. Please try again, or contact the administrator if the problem continues.</p>";
					}
				}
				else {
					$members = getMembers($groupid);
					echo "<p>Select a member below to remove them:</p>";
					echo "<div id=\"removeUser\">";
					while ($row = mysql_fetch_array($members)) {
						if ($row[0] != $_SESSION['username']) {
							echo "<fieldset>";
							echo "<p>Name: " . $row[1] . " " . $row[2] . "</p>";
							echo "<p>Username: " . $row[0] . "</p>";
							echo "<p>Email: " . $row[3] . "</p>";
							if ($row[4] == 'Y') {
								echo "<p>Unable to remove Admin account from the group.</p>";
							}
							else {
								echo "<form name=\"removeForm\" id=\"removeForm\" method=\"post\" action=\"?removeuser&removed\"><input type =\"hidden\" id=\"username\" name=\"username\" value=\"" . $row[0] . "\" /><input type=\"submit\" value=\"Remove\"></form>";
								echo "</fieldset><br .>";
							}
						}
					}
					echo "</div>";
				}
			}
			else {
				echo "<p>You are not the administrator of your Flatshare group and cannot remove other members.</p>";
			}
		}
	}
	else if (isset($_GET['creategroup'])) {
		if (isset($_GET['created'])) {
			include("flatshare_php/group_create.php");
		}
		else if (!checkUserGroup($_SESSION['username'])) {
			include("flatshare_php/group_create_form.php");
		}
		else {
			echo "<p>You are currently in a Flatshare Group. You must leave your current group before you can start a new group.</p>";	
		}
	}
	else if (isset($_GET['leavegroup'])) {
		if (!checkUserGroup($_SESSION['username'])) {
			echo "<p>You are not in a group, and thus are unable to leave.</p>";
		}
		else {
			include("flatshare_php/group_leave.php");
			//echo "<p>You can leave the group!</p>";	
		}
	}
	else if (isset($_GET['makeadmin'])) {
		// code to present users and show button to give "admin" to a user
		$groupid = checkUserGroup($_SESSION['username']);
		if ($groupid == NULL || $groupid == 0) {
			echo "<p>You are not currently a member of a Flatshare Group. You may create a group from the menu on the left, or wait for an invitation to join a group.</p>";
		}
		else {
			if (checkGroupAdmin($groupid, $_SESSION['username'])) {
				if (isset($_GET['done'])) {
					// code to actually remove user from group
					if (setUserAdmin($_POST['username'])) {
						echo "<p>The user was successfully made an admin of the Flatshare Group.</p>";
					}
					else {
						echo "<p>The user was NOT made an admin of the Flatshare Group. Please try again, or contact the administrator if the problem continues.</p>";
					}
				}
				else {
					$members = getMembers($groupid);
					echo "<p>Select a member below to promote them to admin:</p>";
					echo "<div id=\"removeUser\">";
					while ($row = mysql_fetch_array($members)) {
						if ($row[4] == 'N') {
							echo "<fieldset>";
							echo "<p>Name: " . $row[1] . " " . $row[2] . "</p>";
							echo "<p>Username: " . $row[0] . "</p>";
							echo "<p>Email: " . $row[3] . "</p>";
							echo "<form name=\"adminForm\" id=\"adminForm\" method=\"post\" action=\"?makeadmin&done\"><input type =\"hidden\" id=\"username\" name=\"username\" value=\"" . $row[0] . "\" /><input type=\"submit\" value=\"Make Admin\"></form>";
							echo "</fieldset><br />";
						}
					}
					echo "</div>";
				}
			}
		}
	}
	else {
		echo "<p>You may select from the options in the menu to the left to perform any functions related to Flatshare Groups.</p>";
	}
	
?>